package com.cpkso.sprdata.web;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.cpkso.sprdata.domain.Author;
import com.cpkso.sprdata.domain.Book;
import com.cpkso.sprdata.service.BookService;

/**
 * 测试 ： 分页， 关联查询
 * @author wrzhxy@qq.com
 * @date 2017年11月8日
 */
@Controller
@Scope("prototype")
@RequestMapping("/lue")
public class BookController {
	@GetMapping(path="/login")
	public ModelAndView login() {
		return new ModelAndView(new RedirectView("http://www.baidu.com"));
	}
	
	@GetMapping(path="/g/{authorId}")
	@ResponseBody
	public List<Book> look(@PathVariable long authorId) {
System.out.println("== look ==");		
		Author author = new Author();
		author.setId(authorId);
		List<Book> list = bookService.dosomething(author);
System.out.println("== list ==");
		return list;
	}
	
	@PostMapping(path="/h")
	@ResponseBody
	public Book hello(@ModelAttribute  Book book) {
		return bookService.publish(book);
	}
	
	@RequestMapping("/upload")
	public String upload() {
		return "upload";
	}
	
	@GetMapping("/login/{name}")
	@ResponseBody
	public String login(@PathVariable String name, HttpSession session) {
		session.setAttribute("user", name);
		return "登录成功";
	}
	
	@RequestMapping("/hello")
	@ResponseBody
	public String home(HttpSession session) {
		String user = (String) session.getAttribute("user");
		return user == null ? "你好，世界!" : user;
	}
	private BookService bookService;
	@Autowired
	public void setBookService(BookService bookService) {
		this.bookService = bookService;
	}
}
